package com.linkwings.dbTemp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.catalina.util.MD5Encoder;

public class StringUtils {



	/**
	 *
	 * List<String>
	 * @param begin
	 * @param end
	 * @return
	 * @author Sam
	 * Jan 16, 2007
	 */
	public static List<String> getRecentNnmberToString(int begin ,int end){
		if(begin<end){
			String temp;
			List<String> stringList=new ArrayList();
			for (int i = begin; i < end; i++) {
				temp = String.valueOf(i);
				if(temp.length()==1){
					temp="0"+temp;
				}
				stringList.add(temp);
			}
			return stringList;
		}
		return null;
	}




	public static String convert2UTF8(String source) throws Exception {
		if (source == null)
			return null;
		if (source.trim().length() == 0)
			return "";
		int limit = 10;
		String output = source;
		while (limit-- > 0) {
//			System.out.println("----------> output: " + output);
			String newOutput = new String(output.getBytes("8859_1"), "UTF8");
//			System.out.println("----------> newOutput: " + newOutput);
			byte[] newOutputByteArray = newOutput.getBytes("UTF8");
			boolean allInASCII = true;
			for (int i = 0; i < newOutputByteArray.length; i++) {
//				System.out.println("----------> newOutputByteArray: " + newOutputByteArray[i]);
				if (newOutputByteArray[i] <= 0) {
					allInASCII = false;
					break;
				}
			}
			if (allInASCII)
				return output;
			output = newOutput;
		}
		return output;
	}
    public static boolean checkStringNotEmpty(String str){
    	return (str!=null&&str.trim().equals("")==false);
    }
    public static String randomStr(){

		Date now = new Date();
		Random random = new Random();
		StringBuffer str = new StringBuffer("");
		str.append(now.getTime()+"");
		for(int i=1;i<=5;i++){
			int k = random.nextInt();
			int j = Math.abs(k % 25) + 65;
			char c = (char)j;
			str.append(c);
		}
		return str.toString();
	}

	public static List<String> findStringListByPatternMatcherStrContent(String rgx,String content,Integer total){
		Pattern pattern = Pattern.compile(rgx,Pattern.CASE_INSENSITIVE);
		return findStringListByPatternMatcher(pattern,content,total);
	}


	public static List<String> findStringListByPatternMatcher(Pattern pattern,String content,Integer total){
		List<String> list = new ArrayList<String>();
		Matcher m = pattern.matcher(content);
		boolean iskeepfind=true;
		while(m.find()&&iskeepfind==true){
			String temstr = m.group(1);
			list.add(temstr);
			if(total!=null&&list.size()>=total){
				iskeepfind=false;
			}
		}
		return list;
	}

	public static String genUUID(){
		return java.util.UUID.randomUUID().toString().replaceAll("-", "");
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//getRecentNnmberToString(1,19);
		//String testStr="<img src=\"http://cimg2.163.com/cnews/2007/1/24/20070124073355cd976.jpg\" border=\"0\" width=\"160\" height=\"120\" />";
		String testStr="ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss";
		//testStr=ContentRegTools.AutoAddLink(testStr);
		//System.out.print(testStr);
		String str=genUUID();
		System.out.println(str);
	}


	//add by LK
    private static Random randGen = new Random();
  //add by LK
    private static char[] numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" +
                    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
  //add by LK
    public static String randomString(int length) {
        if (length < 1) {
            return null;
        }
        // Create a char buffer to put random letters and numbers in.
        char [] randBuffer = new char[length];
        for (int i=0; i<randBuffer.length; i++) {
            randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
        }
        return new String(randBuffer);
    }


}
